package com.innoking.docsys.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.innoking.docsys.user.dto.UpmsFondsManage;
import com.innoking.docsys.user.service.impl.UpmsFondsServiceImpl;
import com.innoking.docsys.utils.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 全宗管理Controller
 * @author zry
 * 2019-09-11
 */
@Controller
@RequestMapping("/api/fonds")
public class FondsManageController {

	private static final Logger logger = LoggerFactory.getLogger(FondsManageController.class);

    @Autowired
    public UpmsFondsServiceImpl upmsFondsService;
	/**
	 * 查询所有全宗
	 * @return
	 */
	@RequestMapping(value = "/findfondsList")
    @ResponseBody
	public ResultObjVO findfondsList(@RequestBody String data) {
        List<UpmsFondsManage> fondsAll = upmsFondsService.findfondsList();
        if(fondsAll.size() == 0){
            return ResultUtil.error("未查询到全宗信息");
        }
        return ResultUtil.success(fondsAll);
	}
    /**
     * 根据组织机构Code查询全宗
     * @return
     */
    @RequestMapping(value = "/findfondsListByOrgCode")
    @ResponseBody
    public ResultObjVO findOrgListByOrgCode(@RequestBody String data) {
        OtherPojo ob = JsonToOtherUtil.rtOtherString(data);
        String s = ob.getObj().toString();
        JSONObject jsonObject = JSONObject.parseObject(s);
        String orgCode = jsonObject.get("orgCode").toString();
        UpmsFondsManage upmsFondsManage = new UpmsFondsManage();
        if(StringUtils.isNotBlank(orgCode)){
            List<UpmsFondsManage> list = upmsFondsService.findOrgListByOrgCode(orgCode);
            upmsFondsManage = list.get(0);
            if(upmsFondsManage == null){
                return ResultUtil.error("未查询到组织机构信息");
            }
        }else{
            return ResultUtil.error("参数为空");
        }
        return  ResultUtil.success(upmsFondsManage);
    }
    /**
     * 根据全宗Id查询全宗
     * @return
     */
    @RequestMapping(value = "/findfondsListByfondsId")
    @ResponseBody
    public ResultObjVO findfondsListByfondsId(@RequestBody String data) {
        OtherPojo ob = JsonToOtherUtil.rtOtherString(data);
        String s = ob.getObj().toString();
        JSONObject jsonObject = JSONObject.parseObject(s);
        String fondsId = jsonObject.get("fondsId").toString();
        UpmsFondsManage upmsFondsManage = new UpmsFondsManage();
        if(StringUtils.isNotBlank(fondsId)){
            List<UpmsFondsManage> list = upmsFondsService.findfondsListByfondsId(fondsId);
            upmsFondsManage = list.get(0);
            if(upmsFondsManage == null){
                return ResultUtil.error("未查询到组织机构信息");
            }
        }else{
            return ResultUtil.error("参数为空");
        }
        return  ResultUtil.success(upmsFondsManage);
    }
}